import {addFeedback} from '../../api/complaint'
Page({
  data: {
    complaintTypes: [
      { name: '请选择投诉类型'},
      { name: '景区问题', value: '0' },
      { name: '服务态度', value: '1' }
    ],
    selectedComplaint: null,
    images:[],//
    newImages:[],//上传到服务器的图片集合
    complaintIndex: 0,
    formdata:{
      feedType:0,
      feedContent: '',
      feedType: '',
      name: '',
      phone: '',
    }
  },

  bindPickerChange: function(e) {
    console.log(e);
    // 获取用户选择的索引
    const selectedIndex = e.detail.value;
    
    // 如果选择的是第一个"请选择"选项（索引0），则不做处理
    if (selectedIndex === 0) {
      return; // 直接返回，不更新数据
    }
    
    const list = [...this.data.complaintTypes];
    console.log(list[selectedIndex].value);
    
    // 只有选择了有效选项时才更新数据
    this.setData({
      complaintIndex: selectedIndex,
      'formdata.feedType': list[selectedIndex].value
    });
  },

  bindContentInput: function(e) {
    this.setData({
      'formdata.feedContent': e.detail.value
    });
  },

  bindNameInput: function(e) {
    this.setData({
      'formdata.name': e.detail
    });
  },

  bindPhoneInput: function(e) {
    this.setData({
      'formdata.phone': e.detail
    });
  },

  uploadAvatar(filePath) {
    const that = this;
    wx.uploadFile({
      url: 'http://127.0.0.1:8024/wx/upload', 
      filePath: filePath,
      name: 'file',
      formData: {
        'avatarPath': filePath
      },
      success(uploadRes) {
        let data=JSON.parse(uploadRes.data)
        console.log(data)
        that.setData({
          newImages:that.data.newImages.concat(data.data.url)
        })
        console.log("newImg",that.data.newImages)
      },
      fail(err) {
        console.error('头像上传失败', err);
      }
    });
  },

  chooseImage: function() {
    wx.chooseImage({
      count: 3 - this.data.formdata.feedType.length,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        this.setData({
          images: this.data.images.concat(res.tempFilePaths)
        });
        
      }
    });
  },

  deleteImage: function(e) {
    const index = e.currentTarget.dataset.index;
    const images = this.data.formdata.images;
    images.splice(index, 1);
    this.setData({
      images:images,
    });
  },

  //添加反馈
  addFeedback(){
    console.log(this.data.newImages)
    this.setData({
      'formdata.feedImg':this.data.newImages.toString()
    })
    addFeedback(this.data.formdata).then(data=>{
      console.log(data)
    })
  },

  submitForm: function() {
    if(!this.data.formdata.feedContent){
      wx.showToast({
        title: '请输入投诉内容',
        icon: 'none'
      });
      return;
    }
    if (!this.data.formdata.name) {
      wx.showToast({
        title: '请输入联系人姓名',
        icon: 'none'
      });
      return;
    }
    if (!this.data.formdata.phone) {
      wx.showToast({
        title: '请输入联系电话',
        icon: 'none'
      });
      return;
    }
    if (!/^1[3-9]\d{9}$/.test(this.data.formdata.phone)) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none'
      });
      return;
    }
    if(!this.data.formdata.feedType){
      wx.showToast({
        title: '请选择投诉类型',
        icon: 'none'
      });
      return;
    }
    wx.showLoading({
      title: '提交中...',
    });

    // 这里应该是实际的提交逻辑
    setTimeout(() => {
      //将用户上传图片遍历上传服务器
      this.data.images.forEach((item)=>{
        console.log(item)
        this.uploadAvatar(item)
      })
    }, 1500);

    setTimeout(()=>{
      this.addFeedback()
      wx.hideLoading();
      wx.navigateBack()
    },3000)
    
  },

  makePhoneCall: function() {
    wx.makePhoneCall({
      phoneNumber: '400-123-4567' // 记得替换为实际的客服电话
    });
  }
});